<?php

class appRain_Base_Modules_DataGrid extends appRain_Base_Objects
{
	public $rows = Array();
	public $ondatabind;
	public $class_even = 'even';
	public $class_odd = 'odd';
	
	public function __construct()
	{
		$this->ondatabind = App::Load('Module/Event');
	}
	
	public function clear()
	{
		$this->rows = Array();
		
		return $this;
	}
	
	public function AddRow()
	{
		$this->rows[] = new DataGridRow(func_get_args());
		
		return $this;
	}
	
	private function attachHeader()
	{
		$HH = App::Helper('Html');		
		$header = $this->getHeader();
		$html = '';
		if(!empty($header))
		{
			$html .= $HH->getTag('thead');
			$html .= $HH->getTag('tr',Array('class'=>'first last'));
					foreach( $header as $tkey => $tval):
						if( $tval == 'Options'):
							 $html .=  $HH->getTag('th',Array('align'=>'left', 'width' => '100'),$tval);
						else:
							 $html .=  $HH->getTag('th',Array('align'=>'left'),$tval);
						endif;
					endforeach;;
			$html .=  '</tr>';
			$html .=  '</thead>';
		}
		
		return $html;
	}
	
	public function attachFooter()
	{
		$html = "";
		$link = $this->getFooter();
		if(isset($link))
		{
			$HH = App::Helper('Html');		
			$html = $HH->getTag('tfoot');
			$html .= $HH->getTag('tr',Array('class'=>'first'));
				if(is_array($link))
				{
					foreach( $link as $tkey => $tval)
					{
						$html .= $HH->getTag('th',Array('align'=>'left'),$tval);
					}
				}
				else
				{
					$html .= $HH->getTag('td',Array('colspan'=>count($this->getHeader()),'align'=>'center'),$link);
				}
			$html .= $HH->getTag('/tr');
			$html .= $HH->getTag('/tfoot');
		}

		return $html;
	
	}
	
	public function attachBody()
	{
		$HH = App::Helper('Html');
		
		$html = $HH->getTag('tbody');
		foreach($this->rows as $rkey => $row)
		{
			$this->ondatabind->Raise($this, $row);
			$html.= $HH->getTag('tr',Array('id'=>'tr_' . $rkey,'class'=> (($rkey %2 ) ? $this->class_even : $this->class_odd )));			
			if($this->getDisplay() == 'FormListing')
			{
				$hints = isset($row->cells[2]) ?  "<div class=\"hints\">{$row->cells[2]}</div>" : "";
				$html.= "<th valign=\"top\">{$row->cells[0]}</th><td>{$hints}{$row->cells[1]}</td>";
			}
			else
			{
				$html.= '<td>'.implode('</td><td>', $row->cells).'</td>';
			}
			$html.= $HH->getTag('/tr');
		}
		$html .= $HH->getTag('/tbody');
		
		return $html;
	}
	
	public function Render()
	{
		$HH = App::Helper('Html');		
		$html = $this->attachHeader();
		$html .= $this->attachBody();		
		$html .= $this->attachFooter();
		
		if($this->getDisplay() == 'FormListing')echo $HH->getTag('table',Array('width'=>'100%','cellspacing' => '0', 'cellpadding' => '0', 'class' => 'form_listing'),$html);
		else echo $HH->getTag('table',Array('cellspacing' => '0', 'cellpadding' => '0', 'class' => 'data-table'),$html);
	}	
}

class DataGridRow
{
	public $cells;
	function DataGridRow()
	{
		$array = func_get_args();
		$this->cells = $array[0];
	}
}